package com.alon.spring01.concurrent;

import lombok.experimental.Helper;

/**
 * @program: spring01
 * @description: 单例模式
 * @author: yulongguang
 * @create: 2019-04-15 14:14
 **/
public class Singleton {

    /**
     * 单例模式--双重检锁
     */
    private static volatile Singleton singleton;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    return new Singleton();
                }
            }
        }
        return singleton;
    }

    /**
     * 饿汉式（静态常量）
     */
    private static final Singleton INSTANCE = new Singleton();

    public static Singleton getInstance1() {
        return INSTANCE;
    }

    /**
     * 饿汉式（静态代码块）
     */
    private static Singleton singleton01;

    static {
        singleton01 = new Singleton();
    }

    public static Singleton getInstance2() {
        return singleton01;
    }

    /**
     * 静态内部类
     */
    private static class SingletonInstance {

        private static final Singleton INSTANCE01 = new Singleton();
    }

    public static Singleton getInstance3() {
        return SingletonInstance.INSTANCE01;
    }

    /**
     * 枚举
     */


}
